﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EquipmentManager.Entities;

namespace EquipmentManager.BOLogic
{
    public class BOBase
    {
        protected ModelContainer db { get; set; }

        protected User User
        {
            get
            {
                return HttpContext.Current.Session["userSessionKeyName"] as User;
            }
            set
            {
                HttpContext.Current.Session["userSessionKeyName"] = value;
            }
        }

        public BOBase()
        {
            db = new ModelContainer();
        }

        public bool Seed()
        {
            try
            {
                Directorate directorate = new Directorate();
                directorate.Name = "Винницкая";
                directorate.Description = "Винницкая дирекция УДППЗ Укрпочта";
                db.Set<Directorate>().Add(directorate);

                PostOffice postOffice = new PostOffice();
                postOffice.Name = "Аппарат дирекции";
                db.Set<PostOffice>().Add(postOffice);

                User user = new User();
                user.UserName = "Danver";
                user.Password = "skDt6kEJWL/Ou5OOtYFPBEuU6nhTTGXp9BimKPzVgd4=ovO04WC7";
                user.Role = UserRole.Admin;
                user.FullName = "Mr. Danver, Sir";
                user.Status = EntityStatus.Active;
                user.RegistredDate = DateTime.Now;
                db.Set<User>().Add(user);

                Region region = new Region();
                region.Name = "Дирекция";
                db.Set<Region>().Add(region);
                directorate.Regions.Add(region);
                region.PostOffices.Add(postOffice);
                region.Users.Add(user);

                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}